import axios from 'axios'
import { message } from 'ant-design-vue'
import { storage } from '@/utils'

const request = axios.create({
  baseURL: '',
  timeout: 60000,
})

//请求拦截器
const safeURL = ['/captchaImage', '/login']
request.interceptors.request.use((config) => {
  let url = config.url
  //  /api/captchaImage ==> /captchaImage
  url = url.replace(/^\/[^\/]+/, '')
  if (safeURL.includes(url)) {
    config.headers.isToken = 'false'
  } else {
    //需要token的情况
    let token = storage.get('TOKEN')
    if (token) {
      config.headers.Authorization = token
    }
  }
  return config
})

//响应拦截器
request.interceptors.response.use(
  (response) => {
    return response.data
  },
  (reason) => {
    message.error('当前网络繁忙，请稍后重试！')
    return Promise.reject(reason)
  },
)

export default request
